package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.order.dto.OrderDetailResponse;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.form.CancelOrderRequest;
import com.cskaoyan.order.form.CreateOrderRequest;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderCoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @author silic xia
 * @since 2022/12/03 09:11
 */
@RestController
@RequestMapping("/shopping")
public class ShoppingController {

    @Autowired
    OrderCoreService orderCoreService;

    /**
     * @param request
     * @param servletRequest
     * @return ResponseData
     * @description 生成订单
     * URI shopping/order
     * Method post
     * @author silic xia
     * @since 2022/12/03 11:22
     */
    @PostMapping("/order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest) {

        Long uid = getUserId(servletRequest);
        request.setUserId(uid);
        request.setUniqueKey(UUID.randomUUID().toString());
        String orderId = orderCoreService.createOrder(request);
        return new ResponseUtil().setData(orderId);
    }

    private Long getUserId(HttpServletRequest servletRequest) {
        String userInfo = servletRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        return object.getLong("uid");
    }


    /**
     * @param orderListRequest/servletRequest
     * @return ResponseData
     * @description 获取所有订单
     * URI shopping/order
     * Method get
     * @author silic xia
     * @since 2022/12/04 19:01
     */
    @GetMapping("/order")
    public ResponseData getOrder(OrderListRequest orderListRequest, HttpServletRequest servletRequest) {
        Long uid = getUserId(servletRequest);
        orderListRequest.setUserId(uid);

        OrderListResponse orderListResponse = orderCoreService.getOrder(orderListRequest);

        return new ResponseUtil().setData(orderListResponse);
    }

    /**
     * @param orderId
     * @return ResponseData
     * @description 查询订单详情
     * URI /shopping/order/{id} 占位符
     * Method get
     * @author silic xia
     * @since 2022/12/04 22:54
     */
    @GetMapping("/order/{id}")
    public ResponseData getOrderDetails(@PathVariable("id") String orderId, HttpServletRequest servletRequest) {
        Long uid = getUserId(servletRequest);

        OrderDetailResponse orderDetailResponse = orderCoreService.getOrderDetails(orderId, uid);
        return new ResponseUtil().setData(orderDetailResponse);
    }

    /**
     * @param cancelOrderRequest
     * @return ResponseData
     * @description 取消订单
     * URI /shopping/cancelOrder
     * Method post
     * @author silic xia
     * @since 2022/12/04 23:18
     */

    @PostMapping("cancelOrder")
    public ResponseData cancelOrder(@RequestBody CancelOrderRequest cancelOrderRequest) {
        orderCoreService.cancelOrder(cancelOrderRequest.getOrderId());
        return new ResponseUtil<>().setData("成功","success");
    }

    /**
     * @param orderId
     * @return
     * @description 删除订单
     * URI /shopping/order/{id}
     * Method delete
     * @author silic xia
     * @since 2022/12/05 1:28
     */

    @DeleteMapping("order/{id}")
    public ResponseData deleteOrder(@PathVariable("id") String orderId){
        orderCoreService.deleteOrder(String.valueOf(orderId));
        return new ResponseUtil<>().setData("成功","success");
    }
}

